/**
 * Created by zhanglihe on 16/1/15.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'datepicker',
    'highCharts'
], function ($, common, ready, Collection, datepicker, highCharts) {

    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'partner/sell_count',
                condition: {},
                callback: function () {
                    loaded();//移除加载效果
                    var series = _this.collection.result.data.series;
                    var tab = _this.collection.result.data.tab;

                    if (_this.collection.result.count < 1) {
                        $('#data-highcharts').html('<center>无数据</center>');
                    } else {
                        $('#data-highcharts').highcharts({
                            chart: {
                                zoomType: 'xy'
                            },
                            title: {
                                text: ''
                            },
                            xAxis: [{
                                //tickInterval: tickInterval,
                                categories: tab,
                                title: {
                                    text: '时间区间',
                                    style: {
                                        color: '#89A54E'
                                    }
                                },
                                labels: {
                                    rotation: -45,
                                    align: 'right',
                                    style: {
                                        fontSize: '10px'
                                    }
                                }
                            }],
                            yAxis: [{ // Primary yAxis
                                allowDecimals: false,
                                floor: 0,
                                labels: {
                                    format: '{value}',
                                    style: {
                                        color: '#89A54E'
                                    }
                                },
                                title: {
                                    text: '参考值',
                                    style: {
                                        color: '#89A54E'
                                    }
                                }
                            }],
                            tooltip: {
                                shared: true
                            },
                            legend: {
                                layout: 'vertical',
                                align: 'right',
                                verticalAlign: 'middle',
                                backgroundColor: '#FFFFFF'
                            },
                            series: series
                        });
                    }
                }
            });
            this.collection.fetch();
        },

        event: function () {
            var _this = this;
            $('#btn-date-type').find('a').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.date_type = $(this).attr('rel');
                $(this).addClass('active').siblings().removeClass('active');
                _this.collection.reload();
            });
        }
    };
    new index();

});